<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="/node_modules/bootstrap/dist/css/bootstrap.css">
    <link rel="stylesheet" href="/public/css/login.css">
</head>
<body>
    <div class="container main">
        <div class="col-sm-4 col-xs-12">
            <div class="header">
                <a href="/">
                <img src="/public/img/logo.png" alt=""></a>
            </div>
            <h1>用户注册</h1>
            <!-- 表单具有默认的提交行为，默认是同步的，同步表单提交，浏览器会锁死（转圈儿）等待服务器的响应结果
                表单的同步提交之后，无论服务端响应的是什么，都会直接把响应的结果覆盖掉当前页面 -->
            <form id="register_form">
                <div class="form-group">
                    <label for="email">邮箱</label>
                    <input type="email" class="form-control" id="email" placeholder="email" name="email" autofocus>
                </div>
                <div class="form-group">
                    <label for="nickname">昵称</label>
                    <input type="text" class="form-control" id="nickname" name="nickname" placeholder="nickname">
                </div>
                <div class="form-group">
                    <label for="password">密码</label>
                    <input type="password" class="form-control" id="password" name="password" placeholder="password">
                </div>
                <button type="submit" class="btn btn-success btn-block">注册</button>
            </form>
            <div class="message">
                <p>已有账号？ <a href="/login">点击登录</a></p>
            </div>
            <script src="/node_modules/jquery/dist/jquery.js"></script>
            <script>
            $('#register_form').on('submit', function (e) {
                e.preventDefault()
                var formData = $(this).serialize()
                $.ajax({
                    url: '/register',
                    type: 'post',
                    data: formData,
                    dataType: 'json',
                    success: function (data) {
                        var err_code = data.err_code
                        if (err_code === 0) {
                            // window.alert('注册成功！')
                            // 服务端重定向针对异步请求无效
                            window.location.href = '/'
                        } else if (err_code === 1) {
                            window.alert('邮箱或者昵称已存在!')
                        } else if (err_code === 500) {
                            window.alert('服务器忙，请稍后重试！')
                        }
                    }
                })
            })
            </script>
        </div>
    </div>
    
</body>
</html>